/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.workflow.service.impl;

import me.zhengjie.modules.workflow.domain.ActivitiForm;
import me.zhengjie.modules.workflow.service.dto.ActivitiFormDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.workflow.repository.ActivitiFormRepository;
import me.zhengjie.modules.workflow.service.ActivitiFormService;
import me.zhengjie.modules.workflow.service.dto.ActivitiFormQueryCriteria;
import me.zhengjie.modules.workflow.service.mapstruct.ActivitiFormMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wzp
* @date 2021-04-27
**/
@Service
@RequiredArgsConstructor
public class ActivitiFormServiceImpl implements ActivitiFormService {

    private final ActivitiFormRepository activitiFormRepository;
    private final ActivitiFormMapper activitiFormMapper;

    @Override
    public Map<String,Object> queryAll(ActivitiFormQueryCriteria criteria, Pageable pageable){
        Page<ActivitiForm> page = activitiFormRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(activitiFormMapper::toDto));
    }

    @Override
    public List<ActivitiFormDto> queryAll(ActivitiFormQueryCriteria criteria){
        return activitiFormMapper.toDto(activitiFormRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ActivitiFormDto findById(Long id) {
        ActivitiForm activitiForm = activitiFormRepository.findById(id).orElseGet(ActivitiForm::new);
        ValidationUtil.isNull(activitiForm.getId(),"ActivitiForm","id",id);
        return activitiFormMapper.toDto(activitiForm);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActivitiFormDto create(ActivitiForm resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        //表单key
        resources.setCode(IdUtil.fastSimpleUUID());
        return activitiFormMapper.toDto(activitiFormRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ActivitiForm resources) {
        ActivitiForm activitiForm = activitiFormRepository.findById(resources.getId()).orElseGet(ActivitiForm::new);
        ValidationUtil.isNull( activitiForm.getId(),"ActivitiForm","id",resources.getId());
        activitiForm.copy(resources);
        activitiFormRepository.save(activitiForm);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            activitiFormRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ActivitiFormDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ActivitiFormDto activitiForm : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("表单名称", activitiForm.getName());
            map.put("表单编码", activitiForm.getCode());
            map.put("模型id", activitiForm.getModelId());
            map.put("模型名称", activitiForm.getModelName());
            map.put("表单类型（1、定制设计；2-自定义绘制）", activitiForm.getType());
            map.put("表单内容", activitiForm.getContent());
            map.put("定制表单路径", activitiForm.getFormPath());
            map.put("创建者", activitiForm.getCreateBy());
            map.put("更新者", activitiForm.getUpdateBy());
            map.put("创建时间", activitiForm.getCreateTime());
            map.put("修改时间", activitiForm.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}